import { AnyAction } from 'redux';
import * as TYPE from '../action-types';
import Lesson from "@/typings/Lesson";
export interface Lessons {
    loading: boolean;
    list: Lesson[];
    hasMore: boolean;
    offset: number;
    limit: number;
}
export interface HomeState {
    currentCategory: string;
    lessons: Lessons;
};
let initialState: HomeState = {
    currentCategory: 'all',
    lessons: {
        loading: false,
        list: [],
        hasMore: true,
        offset: 0,
        limit: 5,
    }
};

export default function (state: HomeState = initialState, action: AnyAction): HomeState {
    switch (action.type) {
        case TYPE.SET_CURRENT_CATRGRY:
            return { ...state, currentCategory: action.payload }
        case TYPE.SET_LESSONS_LOADING:
            return {
                ...state,
                lessons: { ...state.lessons, loading: action.payload },
            };
        case TYPE.SET_LESSONS:
            return {
                ...state,
                lessons: {
                    ...state.lessons,
                    loading: false,
                    hasMore: action.payload.hasMore,
                    list: [...state.lessons.list, ...action.payload.list],
                    offset: state.lessons.offset + action.payload.list.length,
                },
            };
        case TYPE.REFRESH_LESSONS:
            return {
                ...state,
                lessons: {
                    ...state.lessons,
                    loading: false,
                    hasMore: action.payload.hasMore,
                    list: action.payload.list,
                    offset: action.payload.list.length,
                },
            };
        default:
            return state;
    }
}